package engine;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Vika
 * @author ak2765
 */
final class InvertedWordList {

	private final Map<Integer,IndexedDocument> positiveIndexedDocuments = new HashMap<Integer,IndexedDocument>();
	private final Map<Integer,IndexedDocument> negativeIndexedDocuments = new HashMap<Integer,IndexedDocument>();

	private final Map<String,List<Integer>> indexedPositiveTitleWords = new HashMap<String,List<Integer>>();
	private final Map<String,List<Integer>> indexedPositiveDescriptionWords = new HashMap<String,List<Integer>>();
	
	private final Map<String,List<Integer>> indexedNegativeTitleWords = new HashMap<String,List<Integer>>();
	private final Map<String,List<Integer>> indexedNegativeDescriptionWords = new HashMap<String,List<Integer>>();

	public int getPositiveTitleFrequencies(String word) {
		return getTitleFrequencies(word, this.indexedPositiveTitleWords, this.positiveIndexedDocuments);
	}
	
	public int getNegativeTitleFrequencies(String word) {
		return getTitleFrequencies(word, this.indexedNegativeTitleWords, this.negativeIndexedDocuments);
	}

	public int getPositiveDescriptionFrequencies(String word) {
		return getDescriptionFrequencies(word, this.indexedPositiveDescriptionWords, this.positiveIndexedDocuments);
	}
	
	public int getNegativeDescriptionFrequencies(String word) {
		return getDescriptionFrequencies(word, this.indexedNegativeDescriptionWords, this.negativeIndexedDocuments);
	}

	/**
	 * @param word
	 * @param indecedWords
	 * @param indexedDocument
	 * @return
	 */
	private int getTitleFrequencies(String word, Map<String, List<Integer>> indecedWords, Map<Integer, IndexedDocument> indexedDocument) {
		int wordFrequency = 0;
		List<Integer> documentIds = indecedWords.get(word);
		if (documentIds == null) {
			return 0;
		}
		for (int docId : documentIds) {
			IndexedDocument doc = indexedDocument.get(docId);
			wordFrequency += doc.getTitleFrequencyForWord(word);
		}
		return wordFrequency;
	}
	
	/**
	 * @param word
	 * @param indecedWords
	 * @param indexedDocument
	 * @return
	 */
	private int getDescriptionFrequencies(String word, Map<String, List<Integer>> indecedWords, Map<Integer, IndexedDocument> indexedDocument) {
		int wordFrequency = 0;
		List<Integer> documentIds = indecedWords.get(word);
		if (documentIds == null) {
			return 0;
		}
		for (int docId : documentIds) {
			IndexedDocument doc = indexedDocument.get(docId);
			wordFrequency += doc.getDescriptionFrequencyForWord(word);
		}
		return wordFrequency;
	}


	public void addPositiveDocument(IndexedDocument indexedDocument) {
		this.positiveIndexedDocuments.put(indexedDocument.getDocumentId(), indexedDocument);
		indexWords(indexedDocument.getDocumentId(), indexedDocument.getTitleWords(), indexedPositiveTitleWords);
		indexWords(indexedDocument.getDocumentId(), indexedDocument.getDescriptionWords(), indexedPositiveDescriptionWords);
	}

	public void addNegativeDocument(IndexedDocument indexedDocument) {
		this.negativeIndexedDocuments.put(indexedDocument.getDocumentId(), indexedDocument);
		indexWords(indexedDocument.getDocumentId(), indexedDocument.getTitleWords(), indexedNegativeTitleWords);
		indexWords(indexedDocument.getDocumentId(), indexedDocument.getDescriptionWords(), indexedNegativeDescriptionWords);
	}

	/**
	 * @param documentId
	 * @param words
	 * @param wordIndex
	 */
	private void indexWords(int documentId, List<String> words, Map<String, List<Integer>> wordIndex) {
		for(String word : words) {
			List<Integer> documentIndexes = wordIndex.get(word);
			
			if (documentIndexes == null) {
				documentIndexes = new LinkedList<Integer>();
				wordIndex.put(word, documentIndexes);
			}
			
			documentIndexes.add(documentId);
		}
	}


	
	//word per document
	
	//order of positive words
	
	//title or body ?
	
}
